<?php
set_time_limit(0);
$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('AttachHelper');
$AttachHelper = new AttachHelper($this->UserConfig, 'task');

$model = $this->LoadModel('Public');
$order_list = $model->getList('order_oldinfo','*',array('o_adtime IS NULL AND o_bank_time IS NULL AND o_pact_totime IS NULL AND (o_receivable = 0 OR o_receivable IS NULL)
AND o_sell_time < ?'=> date('Y-m-d',NOW_TIME-5184000),'`o_status` IN(?)'=>array('征信提交','征信查询','初审提交','终审通过')),array(1,10));
$f = fopen(DATA_PATH."/logs".'/task_batchdellog.txt', "a+");
if(!empty($order_list)){
	foreach ($order_list as $key => $value) {
		$do = $OrderHelper->orderInvalid($value['o_app_id'],true);
		$msg = $value['o_number'].'作废1'.$do['msg'].date('Y-m-d H:i:s',NOW_TIME);
		fwrite($f, var_export($msg, true)."\r\n");
	}
}else{
	$order_arr = $model->getList('order','*',array('`o_sell_time` < ?' => NOW_TIME-5184000,'`o_status` =?' =>'征信查询'),array(1,10));
	//$order_arr = $model->getList('order','*',array('`o_sell_time` < ?' => NOW_TIME-5184000,'`o_status` =?' =>'征信查询','`o_finishs_time` =?' =>'0'),array(1,100));
	//$order_arr = $model->getList('order','*',array('`o_sell_time` < ?' => NOW_TIME-5184000,'`o_status` =?' =>'征信查询'),array(1,100));
	//$order_arr = $model->getList('order','*',array('`o_sell_time` < ?' => NOW_TIME-5184000,'`o_status` =?' =>'征信查询'),array(1,10));
	if(!empty($order_arr)){
		foreach ($order_arr as $k => $v){
			$result = $OrderHelper->orderInvalid($v['o_id'],true);
			$msg = $v['o_number'].'作废2'.$result['msg'].date('Y-m-d H:i:s',NOW_TIME);
			fwrite($f, var_export($msg, true)."\r\n");
		}
	}
}
//退回o_sell_time>15天

$back_list = $model->getList('order_oldinfo','*',array('`o_app_status` IN (?)' =>array('初审提交','审核中','终审通过'),'o_adtime IS NULL AND (o_receivable = 0 OR o_receivable IS NULL) AND o_bank_time IS NULL AND o_pact_totime IS NULL AND o_sell_time < ?'=> date('Y-m-d',NOW_TIME-1296000)));

foreach($back_list as $x => $y){
	if($y['o_app_status'] == '终审通过'){
		$check = $model->getRow('order_audit','*',array('`a_status` =?' =>'ok','`a_results` =?' =>'通过','`a_type` IN(?)' =>array('A00001终审审核','终审审核'),'`a_oid` =?' =>$y['o_app_id']),null,array('a_id DESC'));
		if($check['a_details'] !='通融通过不垫资'){
			$final = $OrderHelper->orderBack($y['o_app_id'],'征信查询','征信过期,系统批量退回',array('uid'=>1,'uname' =>'管理员'),'troback','lock');
		}
	}else{
		$final = $OrderHelper->orderBack($y['o_app_id'],'征信查询','征信过期,系统批量退回',array('uid'=>1,'uname' =>'管理员'),'troback','lock');
	}
	$msg = $y['o_number'].$final['msg'].date('Y-m-d H:i:s',NOW_TIME);
	fwrite($f, var_export($msg, true)."\r\n");
}


/*
$nofunding = $model->getList('order_audit','*',array('`a_status` =?' =>'ok','`a_results` =?' =>'通过','`a_details` =?' =>'通融通过不垫资'));
if(!empty($back_list)){
	foreach($back_list as $k => $v){
		$oid_arr[] = $v['o_app_id'];
	}
}else{
	$oid_arr = array();
}
foreach($nofunding as $key => $val){
	$check = $model->getOneRow('order_audit','*',array('`a_status` =?' =>'ok','`a_results` =?' =>'退回','`a_oid` =?' =>$val['a_oid'],'`a_id` >?' => $val['a_id']));
	if(empty($check)){
		$noback[] = $val['a_oid'];
	}
}
$back_oid = array_diff($oid_arr, $noback);
foreach($back_oid as $x => $y){
	$final = $OrderHelper->orderBack($y,'征信查询','征信超时,系统批量退回',array('uid'=>1,'uname' =>'管理员'),'troback','lock');
	$msg = 'oid:'.$y.$final['msg'].date('Y-m-d H:i:s',NOW_TIME);
	fwrite($f,var_export($msg,true)."\r\n");
}
*/
fclose($f);
echo '<p>处理成功</p>';